package com.woniuedu.search.serviec.impl;

import com.woniuedu.search.entity.Teacher;
import com.woniuedu.search.mapper.TeacherMapper;
import com.woniuedu.search.serviec.TeacherService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author yuankaiqiang
 * @since 2022-04-19
 */
@Repository("teacher")
public class TeacherServiceImpl  implements TeacherService {
     @Resource
     private TeacherMapper teacherMapper;

     @Override
     public Page<Teacher> findByAll(String name, Pageable pageable) {
          // 设置高亮,使用默认的highlighter高亮器
          String fields = "title";
          String preTags = "<span style='background-color:yellow'>";
          String postTags = "</span>";
          BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
          boolQueryBuilder.must(QueryBuilders.matchQuery("name",name));
          // 构建检索条件
          NativeSearchQueryBuilder nativeSearchQueryBuilder =
                  new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withPageable(pageable)
                          .withHighlightFields(new HighlightBuilder.Field(fields).preTags(preTags).postTags(postTags))
                  ;
          return teacherMapper.search(nativeSearchQueryBuilder.build());
     }

     @Override
     public void deleteById(String id) {
          teacherMapper.deleteById(id);
     }

     @Override
     public void save(Teacher teacher) {
          teacherMapper.save(teacher);
     }
}
